<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Khoa_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
		
	/**
	 * Get all Categories
	 *
	 * @access	public
	 * @return 	object	Categories
	 */
	function get_all_khoa()
	{
		$this->db->select('khoa.*, giaovien.id as giaovienid, giaovien.tengv');
		$this->db->from('khoa','giaovien');
		$this->db->join('giaovien','giaovien.id = khoa.id_gv');
		$this->db->where('khoa.deleted',0);
		$this->db->order_by('khoa.id','desc');
		$query = $this->db->get();
		return $query->result_array();
	}	
	
	function get_giaovien()
	{
		$this->db->select('id, magv, tengv');
		$this->db->from('giaovien');
		$this->db->order_by('tengv','asc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Get all entries
	 *
	 * @access	public
	 * @return 	object	Entries
	 */
	function add_khoa()
	{
		
		$data = array(
			'makhoa' 			=> $this->input->post('makhoa'),
			'tenkhoa'			=> $this->input->post('tenkhoa'),
			'id_gv'			=> $this->input->post('giaovien'),
			'created_by'	=> $this->user->id,
			'status'		=> $this->input->post('status'),
			'deleted'		=> 0,
		);
		
		$this->db->set($data);
		$this->db->insert('khoa');
	}
	
	function add_user()
	{
		$password = $this->input->post('password');
		$hash = sha1(microtime());
		$salt = $this->config->item('auth');
		$salt = $salt['salt'];

		$password = sha1($salt.$hash.$password);
		$data = array(
			'username' 			=> $this->input->post('username'),
			'password'			=> $password,
			'hash'				=> $hash,
			'email'				=> $this->input->post('email'),
			'fullname'			=> $this->input->post('fullname'),
			'mobile'			=> $this->input->post('didong'),
			'FK_group_id'		=> $this->input->post('group'),
			'active'			=> $this->input->post('status'),
		);
		
		$this->db->set($data);
		$this->db->insert('user');
	}
	
	public function check_makhoa()
	{
		$makhoa = $this->input->post('makhoa');
        $query =  "
			SELECT id, makhoa
			FROM khoa WHERE makhoa ='$makhoa' 
			";
        $result = $this->db->query($query);
        if($result->num_rows() >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	
    
	
	
    
	public function get_khoa_info($id)
	{
		$this->db->select('*');
		$this->db->from('khoa');
		$this->db->where('id', $id);
		$query = $this->db->get();
		return $query->row();
	}
	
	public function edit_khoa($id)
	{
		
		$fields = array(
			'makhoa' 			=> $this->input->post('makhoa'),
			'tenkhoa'			=> $this->input->post('tenkhoa'),
			'id_gv'					=> $this->input->post('giaovien'),
			'edited_by'				=> $this->user->id,
			'status'				=> $this->input->post('status'),
		);
		
		
		$this->db->set($fields);
		
		$this->db->where('id', $id);
		$this->db->update('khoa');
	}
	
	function delete_khoa($id)
	{
		$fields = array(
			'status' 			=> 0,
			'deleted' 			=> 1,
		);
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('khoa');
	}
	

	
	public function change_status_khoa($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('khoa',$data);		
	}
	
	
	public function search_data($keyword)
	{		
		$this->db->select('khoa.*, giaovien.id as giaovienid, giaovien.tengv');
		$this->db->from('khoa','giaovien');
		$this->db->join('giaovien','giaovien.id = khoa.id_gv');
		$this->db->like('khoa.tenkhoa', $keyword);
		$this->db->where('khoa.deleted',0);
		$this->db->order_by('khoa.id','desc');
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}

	public function count_all_khoa()
	{
		$this->db->select('id');
		$this->db->from('khoa');
		return $this->db->count_all_results();
	}
}
// END giaovien_model.php
/* Location: ./application/models/giaovien_model.php */
